﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web_Project
{
    public partial class test6_5_2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            GridViewRow row = GridView2.Rows[e.RowIndex];
            try
            {
                string Cate = e.NewValues["CategoryId"].ToString();
                decimal price = Convert.ToDecimal(e.NewValues["ListPrice"]);

                //将获取到的新值设置到 SqlDataSource 的 UpdateParameters 中
                SqlDataSource3.UpdateParameters["CategoryId"].DefaultValue = Cate;
                SqlDataSource3.UpdateParameters["ListPrice"].DefaultValue = price.ToString();

                // 获取文件上传控件
                FileUpload file = (FileUpload)GridView2.Rows[e.RowIndex].FindControl("FileUpload1");
                if (file.HasFile)
                {
                    // 如果用户上传了新文件
                    // 创建一个 Up_img 类的实例，用于处理图片上传
                    Up_img my_image = new Up_img();
                    string imgurl = my_image.Upload_img(file);
                    SqlDataSource3.UpdateParameters["Image"].DefaultValue = imgurl;
                    Response.Write(imgurl);
                }
                else
                {
                    // 如果用户没有上传新文件
                    // 获取隐藏字段控件，用于获取原来的图片路径
                    HiddenField field = (HiddenField)GridView2.Rows[e.RowIndex].FindControl("HiddenField1");
                    if (field != null)
                    {
                        SqlDataSource3.UpdateParameters["Image"].DefaultValue = field.Value;
                    }
                }
                SqlDataSource3.Update();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }
    }
}